package org.ubik.filtertreetable.tree;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.ubik.filtertreetable.util.Icons;
import org.ubik.filtertreetable.util.Messages;

/**
 * this class build a filter panel. this panel is used with a tree to input user filters 
 * @author dcourtoi and ofournier and rcollin
 *
 */
@SuppressWarnings("serial")
public class FilterPanel extends JPanel {
	
	/** filter text field */
	private JTextField filterTextField;
		
	/** option searchs choices*/
	private JComboBox searchModeCombobox;
	
	/** constant Equals */
	public static final String EQUALS = Messages.getString("gui.common.equals");

	/** constant Contains */
	public static final String CONTAINS = Messages.getString("gui.common.contains");
	
	/** Refresh button */
	private JButton refreshButton;

	/** clear filter button */
	private JButton clearFilterButton;
	
	/** Search button */
	private JButton searchButton;
	
	/** flag dynamic search */
	private boolean dynamicFilter;
	
	/**
	 *  Default constructor
	 * @param dynamicFilter flag dynamic filter
	 */
	public FilterPanel(boolean dynamicFilter){
		this.dynamicFilter = dynamicFilter;
		initComponent();
		initAction();
	}
	
	/**
	 * Init actions
	 */
	private void initAction() {
		filterTextField.addKeyListener(new KeyAdapter(){
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					searchButton.doClick();
				}
			}
		});
	}

	/**
	 * Init GUI components
	 */
	public void initComponent(){
	
		// Layout and border to the main filter panel 
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		this.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));

		// filter text field
		filterTextField = new JTextField(10);
		
		// searchMode
		searchModeCombobox = new JComboBox();
		searchModeCombobox.addItem(CONTAINS);
		searchModeCombobox.addItem(EQUALS);
		
		// clear filter button
		clearFilterButton = new JButton(Icons.DELETE);
		clearFilterButton.setToolTipText(Messages.getString("gui.common.clear"));

		// refresh button
		refreshButton = new JButton(Icons.REFRESH);
		refreshButton.setToolTipText(Messages.getString("gui.common.refresh"));
		
		// search button
		searchButton = new JButton(Icons.SEARCH);
		searchButton.setToolTipText(Messages.getString("gui.common.search"));
		
		//add components to the main panel
		this.add(new JLabel(" "
				+ Messages.getString("gui.common.filter") + " : "));
		this.add(filterTextField);
		
		// searchMode
		this.add(searchModeCombobox);
		
		// if no dynamic mode
		if(!dynamicFilter){
			this.add(searchButton);
		}
		this.add(clearFilterButton);
		this.add(refreshButton);
	}

	public JTextField getFilterTextField() {
		return filterTextField;
	}

	public JButton getRefreshButton() {
		return refreshButton;
	}

	public JButton getClearFilterButton() {
		return clearFilterButton;
	}

	public JButton getSearchButton() {
		return searchButton;
	}

	public boolean isDynamicFilter() {
		return dynamicFilter;
	}

	public JComboBox getSearchModeCombobox() {
		return searchModeCombobox;
	}
}
